<template>
  <div class="login">
    <div class="closeBtn">
      <span class="iconfont iconicon-test"></span>
    </div>

    <div class="logo">
      <span class="iconfont iconnew"></span>
    </div>
    <div class="inputName">
      <authInput
        type="text"
        placeholder="请输入手机号"
        rule="^\d{4,16}$"
        err_massage="请输入正确的手机号码"
        @input="setUserName"
      ></authInput>
    </div>
    <div class="inputPwd">
      <authInput
        type="password"
        placeholder="请输入密码"
        rule="^\d{3,16}$"
        err_massage="请输入正确的密码"
        @input="setPassword"
      ></authInput>
    </div>
    <div class="btn">
      <authBtn text="登陆" @send="sendLogin"></authBtn>
    </div>
    <div>
      <div class="register">
            还没有账号?  <router-link to="/register">立即注册</router-link> 
        </div>
    </div>
  </div>
</template>

<script>
import authInput from "../components/authInput";
import authBtn from "../components/authBtn";
export default {
  components: {
    authInput: authInput,
    authBtn: authBtn
  },
  data() {
    return {
      username: "",
      password: ""
    };
  },
  methods: {
    setUserName(name) {
      this.username = name;
      console.log("父组件收到了用户名的变化");
      console.log(this.username);
    },
    setPassword(pwd) {
      this.password = pwd;
      console.log("父组件收到了密码的变化");
      console.log(this.password);
    },
    sendLogin() {
      console.log("登陆按钮被点击了");
      console.log(`应该发送${this.username},${this.password}`);
      this.$axios({
        url: "/login",
        method: "post",
        data: {
          username: this.username,
          password: this.password
        }
      }). then(res => {
        // 登陆成功逻辑
          // console.log(res.data.statusCode);
          if(!res.data.statusCode){
            // 登陆成功应该是跳转到其他页面,之前仅仅是弹窗提示登陆失败或成功
            //  这里先跳转到个人中心页
            // 保存数据
            localStorage.setItem('token',res.data.data.token);
            localStorage.setItem('user_id',res.data.data.user.id);
            this.$toast.success(res.data.message);
            // 设定时间跳转到其他页面，用到编程式导航
            setTimeout(()=>{
             this.$router.push({
              //  path: '/profile'
             
              // path:'/'
              name: 'homePage'
             })
            },1000)
          }
        });
       
    }
  }
};
</script>

<style lang='less' scoped>
.login {
  padding: 5.556vw;
}
.closeBtn {
  padding-top: 5.556vw;
  .iconfont {
    font-size: 7.5vw;
  }
}
.logo {
  display: flex;
  justify-content: center;
  .iconfont {
    font-size: 35vw;
    color: #d81e06;
  }
}
.btn {
  margin-top: 20.556vw;
}
.register {
        text-align: center;
        margin-top: 6.667vw;
    }
    
</style>